package com.fingard.dsp.bank.directbank.ybt12;

import com.fingard.dsp.bank.directbank.PaymentState;
import com.fingard.dsp.bank.directbank.bankInterface.IBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.ReqBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.ReqQryBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.RetAtsBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.RetAtsQryBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.detail.RetAtsBatPayRecDetail;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * 中意银保通批量代收付
 * 
 * @author Cruise
 * @date 2019/4/25
 */
public class YBT12BatPayRec extends YBT12Base implements IBatPayRec {
	@Override
	public RetAtsBatPayRec d_1988_BatPay(ReqBatPayRec p_req1988) throws Exception {
		return d_BatPayRec(p_req1988);
	}

	@Override
	public RetAtsQryBatPayRec d_1998_QryBatPay(ReqQryBatPayRec p_req1998) throws Exception {
		return d_QryBatPayRec(p_req1998);
	}

	@Override
	public RetAtsBatPayRec d_9188_BatPay(ReqBatPayRec p_req9188) throws Exception {
		return d_BatPayRec(p_req9188);
	}

	@Override
	public RetAtsQryBatPayRec d_9198_QryBatPay(ReqQryBatPayRec p_req9198) throws Exception {
		return d_QryBatPayRec(p_req9198);
	}
	
	/**
	 * 批量代收付批次提交
	 * 
	 * @param p_reqBat
	 * @return
	 * @throws Exception
	 */
	public RetAtsBatPayRec d_BatPayRec(ReqBatPayRec p_reqBat) throws Exception {
		RetAtsBatPayRec retAts = new RetAtsBatPayRec(p_reqBat.reqHeader);
		
		try {
			int tmpSeqNo = 0;
			while(p_reqBat.readRecord()) {
				LinkedList<Object> params = new LinkedList<Object>();
				
				params.add(p_reqBat.readedDetail.askNumber);
				String oppAct = "";
				String oppName = "";
				if ("1988".equals(p_reqBat.reqHeader.transType)) {
					oppAct = p_reqBat.readedDetail.recAct;
					oppName = p_reqBat.readedDetail.recName;
				} else {
					oppAct = p_reqBat.readedDetail.payAct;
					oppName = p_reqBat.readedDetail.payName;
				}
				params.add(oppName);
				params.add(oppAct);
				params.add(p_reqBat.readedDetail.bkName);
				params.add(p_reqBat.readedDetail.lioperType);
				params.add(p_reqBat.readedDetail.sourceNoteCode);
				params.add(p_reqBat.readedDetail.payAmount);
				params.add(p_reqBat.readedDetail.branchCode);
				params.add(new Timestamp(p_reqBat.getPayDateTime().getTime()));
				params.add("");
				params.add("");
				params.add(p_reqBat.readedDetail.channel);
				String payType = "";
				if ("1988".equals(p_reqBat.reqHeader.transType)) {
					payType = "1";
				} else {
					payType = "2";
				}
				params.add(payType);
				params.add(new Timestamp(p_reqBat.getPayDateTime().getTime()));
				params.add("");
				params.add("");
				params.add("");
				params.add(p_reqBat.reqBatchNO);
				params.add(++tmpSeqNo);
				params.add("0");
				params.add("");
				params.add(p_reqBat.readedDetail.getOppProvinceAndCity2()[0]);
				params.add(p_reqBat.readedDetail.getOppProvinceAndCity2()[1]);
				params.add(p_reqBat.readedDetail.certType);
				params.add(p_reqBat.readedDetail.certNum);
				params.add(p_reqBat.readedDetail.cellPhone);
				params.add(p_reqBat.readedDetail.paymentCode);
				params.add(p_reqBat.ownAct);
				params.add(p_reqBat.readedDetail.termID);
				
				StringBuilder sbLog = new StringBuilder();
				boolean saved = saveEachRecords(params, sbLog);
				if(!saved){
					throw new Exception(sbLog.toString());
				}
				
				retAts.respCode = "0";
				retAts.respInfo = "数据已插入中间表";
				retAts.reqSeqID = p_reqBat.reqBatchNO;
				retAts.transState = "YBT+inserted";
				retAts.payInfoCode = "inserted";
			}	
		} catch (Exception ex) {
			ex.printStackTrace();
			retAts.respCode = "-1";
			retAts.respInfo = ex.getMessage();
			WriteBankLogLn(ex);
		}
		
		retAts.writeRetToATS();
		return retAts;
	}
	
	/**
	 * 批量代收付状态查询
	 * 
	 * @param p_reqQryBat
	 * @return
	 * @throws Exception
	 */
	public RetAtsQryBatPayRec d_QryBatPayRec(ReqQryBatPayRec p_reqQryBat) throws Exception {
		RetAtsQryBatPayRec retAts = new RetAtsQryBatPayRec(p_reqQryBat.reqHeader);
		retAts.reqSeqID = p_reqQryBat.reqBatchNO;
		
		try {
            int tmpBatchCouts = getResultRecordCounts(p_reqQryBat.reqBatchNO);
            if (tmpBatchCouts == 0) {
                retAts.respCode = "0";
                retAts.respInfo = "记录不存在";
                retAts.transState = PaymentState.FAIL_Batch;
                retAts.payInfoCode = "E1302";
                retAts.writeAfterDetail();
                return retAts;
            }

			List<Map<String,Object>> tmpRespRecords = new ArrayList<Map<String,Object>>();
			tmpRespRecords = getResultRecords(p_reqQryBat.reqBatchNO);

			if(tmpRespRecords != null && tmpRespRecords.size() > 0) {
				retAts.respCode = "0";
				
	            for(Map<String,Object> eachRecord:tmpRespRecords) {
	                RetAtsBatPayRecDetail tmpRetDetail = new RetAtsBatPayRecDetail();
					
				    tmpRetDetail.postScript = String.valueOf(eachRecord.get("ID"));
				    tmpRetDetail.payAmount = String.valueOf(eachRecord.get("BKAMT1"));;
				    
				    if ("1998".equals(p_reqQryBat.reqHeader.transType)) {
				    	tmpRetDetail.recAct = String.valueOf(eachRecord.get("BKACCTNO"));
				    	tmpRetDetail.recName = String.valueOf(eachRecord.get("BKCUSTNAME"));
				    } else {
				    	tmpRetDetail.payAct = String.valueOf(eachRecord.get("BKACCTNO"));
				    	tmpRetDetail.payName = String.valueOf(eachRecord.get("BKCUSTNAME"));
				    }
				    
				    String tmpRetCode = String.valueOf(eachRecord.get("BKRETCODE"));
				    if ("00000".equals(tmpRetCode)) {
				    	tmpRetDetail.result = "YBT+2";
				    } else {
				    	tmpRetDetail.result = "YBT+3";
				    }
					tmpRetDetail.payInfoCode = tmpRetCode;
					tmpRetDetail.memo = String.valueOf(eachRecord.get("BKRETMSG"));
					
	        		retAts.writeRetDetail(tmpRetDetail);
	            }
	        } else {
	        	retAts.respCode = "-1";
				retAts.respInfo = "银行查询结果文件内容为空!";
				WriteBankLogLn2("没有符合条件的记录!");
	        }
		} catch (Exception ex) {
			ex.printStackTrace();
			retAts.respCode = "-1";
			retAts.respInfo = ex.getMessage();
			WriteBankLogLn(ex);
		}
		
		retAts.writeAfterDetail();
		return retAts;
	}
}
